package com.googlecode.stockhamster.client.swing;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.company.ICompany;
import com.googlecode.stockhamster.expert.IExpert;
import com.googlecode.stockhamster.rumor.IRumor;
import com.googlecode.stockhamster.utils.XFireStub;

public class RumorPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4483837128714290930L;

	private IRumor rumor;
	private ICompany company;
	private IExpert expert;

	private JPanel companyPanel;
	private JScrollPane tablePanel;

	private JTextField name;
	private JTextField value1;
	private JTextField value2;
	private JTextField value3;
	private JTextField value4;

	private JLabel rumorResult;
	private JTable rumorTable;

	private static final String[] rumorTableColumn = { "Company",
			"Increasement ratio 1", "Increasement ratio 2",
			"Increasement ratio 3", "Category ratio 1", "Category ratio 2",
			"Category ratio 3", "Expert 1", "Expert 2", "Expert 3" };

	public RumorPanel() throws MalformedURLException {
		rumor = new XFireStub<IRumor>(Constant.rumorUrl, IRumor.class)
				.getStub();
		company = new XFireStub<ICompany>(Constant.companyUrl, ICompany.class)
				.getStub();
		expert = new XFireStub<IExpert>(Constant.expertUrl, IExpert.class)
				.getStub();

		setLayout(new GridLayout(2, 1));
		companyPanel = new JPanel();
		companyPanel.add(new JLabel("Name:"));
		name = new JTextField(8);
		companyPanel.add(name);
		companyPanel.add(new JLabel("Value:"));
		value1 = new JTextField(5);
		companyPanel.add(value1);
		value2 = new JTextField(5);
		companyPanel.add(value2);
		value3 = new JTextField(5);
		companyPanel.add(value3);
		value4 = new JTextField(5);
		companyPanel.add(value4);
		JButton asCompany = new JButton("As company");
		asCompany.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (rumor.makeRumorCompany(Constant.rumorAdminCode, name
						.getText(), Double.valueOf(value1.getText()), Double
						.valueOf(value2.getText()), Double.valueOf(value3
						.getText()), Double.valueOf(value4.getText())))
					rumorResult.setText("As company: succeed");
				else
					rumorResult.setText("As company: failed");
			}
		});
		companyPanel.add(asCompany);
		JButton asCategory = new JButton("As category");
		asCategory.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (rumor.makeRumorCategory(Constant.rumorAdminCode, name
						.getText(), Double.valueOf(value1.getText()), Double
						.valueOf(value2.getText()), Double.valueOf(value3
						.getText()), Double.valueOf(value4.getText())))
					rumorResult.setText("As category: succeed");
				else
					rumorResult.setText("As category: failed");
			}
		});
		companyPanel.add(asCategory);
		JButton refresh = new JButton("Refresh");
		refresh.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String names[] = company
						.getCompanyList(Constant.companyAdminCode);
				Object content[][] = new Object[names.length][10];
				for (int i = 0; i < names.length; i++) {
					String category = company.getCategory(names[i]);
					Integer stockId = company.getStockID(names[i]);

					Double co1 = rumor.getCompanyRatio(1, names[i]);
					Double co2 = rumor.getCompanyRatio(2, names[i]);
					Double co3 = rumor.getCompanyRatio(3, names[i]);
					Double ca1 = rumor.getCategoryRatio(1, category);
					Double ca2 = rumor.getCategoryRatio(2, category);
					Double ca3 = rumor.getCategoryRatio(3, category);
					Long capital = company.getCapital(stockId);
					Integer size = company.getIssueSize(stockId);

					content[i][0] = names[i];
					content[i][1] = co1;
					content[i][2] = co2;
					content[i][3] = co3;
					content[i][4] = ca1;
					content[i][5] = ca2;
					content[i][6] = ca3;
					content[i][7] = expert.evaluation(capital, size, co1, ca1);
					content[i][8] = expert.evaluation(capital, size, co2, ca2);
					content[i][9] = expert.evaluation(capital, size, co3, ca3);
				}
				rumorTable = new JTable(content, rumorTableColumn);
				tablePanel.setViewportView(rumorTable);
			}
		});
		companyPanel.add(refresh);
		rumorResult = new JLabel();
		companyPanel.add(rumorResult);
		rumorTable = new JTable(new Object[0][7], rumorTableColumn);
		tablePanel = new JScrollPane(rumorTable);
		add(companyPanel);
		add(tablePanel);
	}
}
